import copy
from time import time
from multiprocessing import Process, Queue
from utils.noticeUtils.dingtalkControl import DingTalkSendMsg
from utils.readFileUtils.excel_control import write_excle_data
from public_way import public_invoking
from utils.times_tool.time_control import now_time
from concurrent.futures import ThreadPoolExecutor, Future


class Testishotdata:

    """
    Listing 迁移
    """

    public_program = public_invoking()

    # 全量数据初始化服务验证
    def base_comparison(self, id):

        star_time = time()

        try:
            id_lis = id.split(",")
        except:
            id_lis = [id]

        # 将公共方法实例化为对象
        public_program = public_invoking()

        # 获取需要验证数据
        erp_sql = f"SELECT OrderSourceSKU, ASINorItemID, SKU FROM RB_Amazon_Product_Base(nolock) " \
                  f"WHERE CustomerID = 1 AND OrderSourceID in ({id})  AND OrderSourceSKUID > 0 AND AddTime < '2023-03-01';"
        erp_data = public_program.erp_db(erp_sql)
        # 获取来源渠道对应的部门表ID
        ordersource_sql = f"select DepartmentId from aml_department_ordersource where OrderSourceId in ({id})"
        cold_id = public_program.cold_db(ordersource_sql)
        # 对部门表ID进行去重
        lis = []
        for i in cold_id:
            if i not in lis:
                lis.append(i)
        # 推导式将字典的key值存进列表
        ordersource_base = [n["DepartmentId"] for n in lis]
        cold_data = []
        # 遍历获取不同部门表的渠道数据
        for j in ordersource_base:
            sql = f"SELECT OrderSourceSKU, ASINorItemID, SKU FROM aml_amazon_product_base_dep{j}" \
                  f" WHERE OrderSourceID in ({id});"
            data = public_program.cold_db(sql)
            cold_data += data

        _erpdata = []
        for j in erp_data:
            # if j["ListingPrice"] != None:
            #     j["ListingPrice"] = float(j["ListingPrice"])
            # j["SalePrice"] = float(j["SalePrice"])
            _erpdata.append(j)

        # 创建线程池
        err = []
        pool = ThreadPoolExecutor(10)
        for i in cold_data:
            # if i["ListingPrice"] != None:
            #     i["ListingPrice"] = float(i["ListingPrice"])
            # i["SalePrice"] = float(i["SalePrice"])
            future = pool.submit(public_program.threading_pool_comparison, i, _erpdata)
            if future.result():
                err.append(future.result())
        pool.shutdown(True)

        end_time = time()
        execute_time = round((end_time - star_time), 2)


        if len(err) > 0:
            dingtalk_report = f"全量数据初始化共检测{len(id_lis)}个来源渠道  \n" \
                              f"程序执行时间{execute_time}s    \n" \
                              f"共检测大宽表{len(erp_data)}条数据    \n" \
                              f"共检测冷数据表{len(cold_data)}条数据    \n" \
                              f"发现{len(err)}条异常数据    \n" \
                              f"异常数据明细:               \n" \
                              f"{err}"
            # DingTalkSendMsg().send_migration_report(dingtalk_report)
        else:
            dingtalk_report = f"全量数据初始化共检测共检测{len(id_lis)}个来源渠道  \n" \
                              f"程序执行时间{execute_time}s    \n" \
                              f"共检测{len(erp_data)}条数据    \n" \
                              f"共检测{len(cold_data)}条数据    \n" \
                              f"发现{len(err)}条异常数据"
            # DingTalkSendMsg().send_migration_report(dingtalk_report)

        return dingtalk_report

    # 冷热状态初始化服务验证
    def cold_hot_comparison(self, id, hot_sql, check=True):

        # 先判断数据初始化全量同步是否正常
        if check == True:
            # 创建队列
            q = Queue()
            eq = Queue()

            # 获取热数据sql并将OrderSourceID替换进去
            filedir = "D:\web_autotest\zhcx-service-test\Common\#热数据sql.csv"
            content = self.public_program.read_file(filedir).split('\n')
            content_sql = []
            for a in content:
                content_sql.append(a.replace("+", id))
            # 获取erp数据(去重)
            data_list = []
            for i in content_sql:
                data = self.public_program.erp_db(i)
                for j in data:
                    if j not in data_list:
                        j["ListingPrice"] = float(j["ListingPrice"])
                        data_list.append(j)
            sum_data = len(data_list)
            # 获取热数据表数据并推到队列
            hot_data = self.public_program.cold_db(hot_sql)
            for i in hot_data:
                # i["ListingPrice"] = float(i["ListingPrice"])
                q.put(i)

            # 创建进程
            self.public_program.process_work(q, eq, data_list)

            error_list = []
            while not eq.empty():
                error_data = eq.get()
                for j in error_data:
                    error_list.append(j)

            if len(error_list) > 0:
                dingtalk_report = f'''热数据初始化共检测{sum_data}条数据 \n''' \
                                  f'''发现{len(error_list)}条异常数据  \n''' \
                                  f'''异常数据明细:   \n{error_list}'''
                DingTalkSendMsg().send_migration_report(dingtalk_report)
            else:
                dingtalk_report = f'''热数据初始化共检测{sum_data}条数据    \n''' \
                                  f'''存在异常数据{len(error_list)}条  \n'''
                DingTalkSendMsg().send_migration_report(dingtalk_report)
        else:
            pass


if __name__ == '__main__':

    id = '17512,17513,17559,17752,17753,17754,17755,17778,17788,17790,17791,17793,17889,17917,17919,18012,18051,18093,18096,18101'
    res = Testishotdata().base_comparison(id)
    print(res)
